package com.cuntian.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cuntian.contant.Constants;
import com.cuntian.dto.PageDTO;
import com.cuntian.entity.Friend;
import com.cuntian.exception.GlobalException;
import com.cuntian.mapper.FriendMapper;
import com.cuntian.model.IMSessionInfo;
import com.cuntian.service.IFriendService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cuntian
 * @since 2025-02-01
 */
@Service
public class FriendServiceImpl extends ServiceImpl<FriendMapper, Friend> implements IFriendService {

    @Override
    public void addFriend(Long fid) {
       IMSessionInfo imSessionInfo = (IMSessionInfo) StpUtil.getSession().get(Constants.USER_SESSION);
       if(!Objects.isNull(isFriend(imSessionInfo.getUserId(),fid))){
            throw new GlobalException("已经是好友关系");
       }
       Friend friend = new Friend();
       friend.setUid(imSessionInfo.getUserId());
       friend.setFid(fid);
       friend.setStatus(Constants.FRIENDSHIP_NOT_CONFIRMED);
       this.save(friend);
    }

    @Override
    public void delFriend(Long fid) {
        IMSessionInfo imSessionInfo = (IMSessionInfo) StpUtil.getSession().get(Constants.USER_SESSION);
        Friend friend = isFriend(imSessionInfo.getUserId(), fid);
        if(!Objects.isNull(friend)){
            throw new GlobalException("不是好友关系");
        }
        friend.setStatus(Constants.FRIENDSHIP_DELETED);
        this.updateById(friend);
        // 同时需要好友的那一条修改状态
        LambdaUpdateWrapper<Friend> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(Friend::getUid,fid);
        updateWrapper.eq(Friend::getFid,imSessionInfo.getUserId());
        updateWrapper.set(Friend::getStatus,Constants.FRIENDSHIP_DELETED);
        this.update(updateWrapper);
    }

    // 根据uid fid 查询是好友关系的
    public Friend isFriend(Long uid,Long fid){
        LambdaQueryWrapper<Friend> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Friend::getUid,uid);
        queryWrapper.eq(Friend::getFid,fid);
        queryWrapper.eq(Friend::getStatus,Constants.FRIENDSHIP_CONFIRMED);
        return this.getOne(queryWrapper);
    }

    @Override
    public Page<Friend> selFriendRequest(PageDTO pageDTO) {
        IMSessionInfo imSessionInfo = (IMSessionInfo) StpUtil.getSession().get(Constants.USER_SESSION);
        LambdaQueryWrapper<Friend> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Friend::getUid,imSessionInfo.getUserId());
        queryWrapper.eq(Friend::getStatus,Constants.FRIENDSHIP_NOT_CONFIRMED);
        Page<Friend> rowRage =new Page<>(pageDTO.getPage(),pageDTO.getPageSize());
        return this.baseMapper.selectPage(rowRage, queryWrapper);
    }

    @Override
    public void confirmFriendRequest(Long shipId) {
        IMSessionInfo imSessionInfo = (IMSessionInfo) StpUtil.getSession().get(Constants.USER_SESSION);
        LambdaQueryWrapper<Friend> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Friend::getShipId,shipId);
        Friend one = this.getOne(queryWrapper);
        if(!one.getFid().equals(imSessionInfo.getUserId())){
            throw new GlobalException("不是你的好友请求");
        }
        one.setStatus(Constants.FRIENDSHIP_CONFIRMED);
        this.updateById(one);
        // 在添加一条数据 将两个id进行交换 方便查询好友
        Friend friend = new Friend();
        friend.setUid(imSessionInfo.getUserId());
        friend.setFid(one.getUid());
        friend.setStatus(Constants.FRIENDSHIP_CONFIRMED);
        this.save(friend);
    }

    @Override
    public Page<Friend> selMyFriend(PageDTO pageDTO) {
        IMSessionInfo imSessionInfo = (IMSessionInfo) StpUtil.getSession().get(Constants.USER_SESSION);
        LambdaQueryWrapper<Friend> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Friend::getUid,imSessionInfo.getUserId());
        queryWrapper.eq(Friend::getStatus,Constants.FRIENDSHIP_CONFIRMED);
        Page<Friend> rowPage = new Page<>(pageDTO.getPage(), pageDTO.getPageSize());
        return this.baseMapper.selectPage(rowPage,queryWrapper);
    }
}
